//
//  HomeHeader.m
//  体育商城
//
//  Created by  on 2022/9/24.
//

#import "HomeHeader.h"
#import "HomeHeaderCell.h"
@interface HomeHeader()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) NSArray<Goods*> *array;
@property (nonatomic,assign) NSInteger count;
@end
@implementation HomeHeader
- (void)awakeFromNib{
    [super awakeFromNib];
    self.count = 5;
    self.array = [GoodsTool find:@"全部" count:self.count];
    WS(self)
    self.segView.block = ^(NSString * _Nonnull text) {
        if ([text isEqualToString:LS(@"全部")]) {
            weakself.array = [GoodsTool find:@"全部" count:self.count];
        }else if ([text isEqualToString:LS(@"球鞋")]){
            weakself.array = [GoodsTool find:@"球鞋" count:self.count];
        }else if ([text isEqualToString:LS(@"篮球")]){
            weakself.array = [GoodsTool find:@"篮球" count:self.count];
        }else if ([text isEqualToString:LS(@"足球")]){
            weakself.array = [GoodsTool find:@"足球" count:self.count];
        }else if ([text isEqualToString:LS(@"足球衫")]){
            weakself.array = [GoodsTool find:@"足球衫" count:self.count];
        }
    };
    [self.collectionView registerNib:[HomeHeaderCell nib] forCellWithReuseIdentifier:[HomeHeaderCell ID]];
}
- (void)setArray:(NSArray<Goods *> *)array{
    _array = array;
    [self.collectionView reloadData];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.array.count > self.count ? self.count : self.array.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeHeaderCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeHeaderCell ID] forIndexPath:indexPath];
    cell.url = [self.array[indexPath.row] mainImage];
    return  cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BuyViewController *vc = [[BuyViewController alloc] init];
    vc.model = self.array[indexPath.row];
    [[self currentVC].navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(96, 96);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 20, 0, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

@end
